<template>
  <div class="mnt-dialog form-container">
    <el-form ref="form" :model="formModel" label-width="160px" :rules="rules">
      <el-row style="text-align: center">
        <el-col :span="12">
          <el-form-item label="工厂:" prop="plantNo">
            <el-select v-model="formModel.plantNo" clearable filterable>
              <el-option
                v-for="item in plantOptions"
                :key="item.plantNo"
                :label="item.plantNo+'-'+item.plantNameC"
                :value="item.plantNo"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="装配单类型:" prop="modelFileNameKey">
            <hey-select v-model="formModel.modelFileNameKey" :in-clude-keys="['Car_BOM','Car_BOM_Model','Car_Door','Car_Meter','Car_Power','Car_Axle','Car_Subframe','Car_Frame','Car_Cow']" clcd="print_model_cd" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="打印顺序:" prop="printOrder">
            <el-input v-model="formModel.printOrder" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="配置代码:" prop="configNo">
            <el-select
              v-model="formModel.configNo"
              clearable
              filterable
            >
              <el-option
                v-for="item in configNoOptions"
                :key="item.configNo"
                :label="item.configNo+'-'+item.configDesc"
                :value="item.configNo"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="submit-area">
      <el-col :span="24">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSubmit()">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>

export default {
  name: 'PartTrackConfigMnt',
  props: ['pageType', 'editData'],
  data() {
    return {
      plantOptions: [],
      configNoOptions: [],
      tableData: [],
      formModel: {
        assembleFormId: '',
        plantNo: '',
        modelFileNameKey: '',
        printOrder: '',
        configNo: ''
      },
      rules: {}
    }
  },
  computed: {},
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.$form = this.$refs.form
      const { pageType, editData, formModel } = this
      const param = { status: '1' }
      if (pageType !== 'add') {
        this.assignData(editData, formModel)
      }
      this.getPlantOptions(param)
      this.getConfigNoOptions()
      this.initRules()
    },
    initRules() {
      const REQUIRED_MSG = this.CONST.REQUIRED_MSG
      this.rules = {
        plantNo: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        modelFileNameKey: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        printOrder: this.HEY_VALIDATOR({ type: ['MAXINT[2]'], required: true }),
        configNo: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }]
      }
      this.$nextTick(_ => {
        this.$form.clearValidate()
      })
    },
    // 获取工厂列表
    getPlantOptions(param) {
      this.get('/tmBasPlant/queryTmBasPlantList', param).then(res => {
        this.plantOptions = res
      })
    },
    // 车辆配置选装配置代码列表(去重)
    getConfigNoOptions() {
      this.get('/vehicleConfigOptional/listVehicleConfigOptionalConfigNo').then(res => {
        this.configNoOptions = res
      })
    },
    /** 修改新增提交 */
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.formModel.assembleFormId !== '') {
            const tip = '修改成功！'
            const url = '/assembleForm/updateAssembleForm'
            this.post(url, this.formModel).then(res => {
              this.$info(tip, () => {
                this.$emit('mntSuc', res)
                this.closeDialog()
              })
            })
          } else {
            const url = '/assembleForm/saveAssembleForm'
            this.post(url, this.formModel).then(res => {
              const tip = '新增成功！'
              this.$info(tip, () => {
                this.$emit('mntSuc', res)
                this.closeDialog()
              })
            })
          }
        }
      })
    },
    closeDialog() {
      this.$parent.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
